package com.g7.dsp.forprotocol.ui.integrated;

import org.eclipse.swt.widgets.Display;

public class DisplayThread extends Thread {
	private volatile boolean stop = false;

	private Display display;
	Object sem = new Object();

	public void run() {
		synchronized (sem) {
			display = Display.getDefault();
			sem.notifyAll();
		}
		System.out.println("====="+Thread.currentThread().getName());
		swtEventLoop();
	}

	private void swtEventLoop() {
		while (!stop) {
				if (!display.isDisposed()) {
					if (!display.readAndDispatch()) {
						display.sleep();
					}
				}
		}
	}

	public Display getDisplay() {
		try {
			synchronized (sem) {
				while (display == null)
				sem.wait();
				return display;
			}
		} catch (Exception e) {
			return null;
		}
	}
	
	public void setStop(boolean stop) {
		this.stop = stop;
	}
}